import torch

# (4,4)
image = torch.tensor(
    [
        [1, 2, 3,4],
        [5, 6, 7,8],
        [9, 10, 11,12],
        [13, 14, 15,16]]
)
# (2,2)
kernel = torch.tensor(
    [
        [1, 0],
        [0, -1]
    ]
)
h = w = image.shape[0]-kernel.shape[0]+1
result = torch.zeros((h,w))
for row in range(h):
    for col in range(w):
        # print(image[row:row+2,col:col+2])
        a=torch.sum(image[row:row+2,col:col+2]*kernel)
        result[row][col]=a
print(result)
